﻿
namespace Content.Web
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;


    // Implements application logic using the eLearningEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class ContentDomainService : LinqToEntitiesDomainService<eLearningEntities>
    {
        public IQueryable<ContentRow> GetContentRowsForUnit(Guid unitID)
        {
            return this.ObjectContext.ContentRows.Where(b => b.UnitID == unitID).OrderBy(b => b.Ordinal);
        }

        public IQueryable<BulletPoint> GetBulletPointsForUnit(Guid unitID)
        {
            return this.ObjectContext.BulletPoints.Where(b => b.UnitID == unitID).OrderBy(b => b.Ordinal);
        }

        public Unit GetUnit(Guid unitID)
        {
            return this.ObjectContext.Units.Single(t => t.UnitID == unitID);
        }

        [Query(IsDefault = true)]
        public IQueryable<BulletPoint> GetBulletPoints()
        {
            return this.ObjectContext.BulletPoints;
        }

        public void InsertBulletPoint(BulletPoint bulletPoint)
        {
            if ((bulletPoint.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(bulletPoint, EntityState.Added);
            }
            else
            {
                this.ObjectContext.BulletPoints.AddObject(bulletPoint);
            }
        }

        public void UpdateBulletPoint(BulletPoint currentBulletPoint)
        {
            this.ObjectContext.BulletPoints.AttachAsModified(currentBulletPoint, this.ChangeSet.GetOriginal(currentBulletPoint));
        }

        public void DeleteBulletPoint(BulletPoint bulletPoint)
        {
            if ((bulletPoint.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(bulletPoint, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.BulletPoints.Attach(bulletPoint);
                this.ObjectContext.BulletPoints.DeleteObject(bulletPoint);
            }
        }

        [Query(IsDefault = true)]
        public IQueryable<ContentRow> GetContentRows()
        {
            return this.ObjectContext.ContentRows;
        }

        public void InsertContentRow(ContentRow contentRow)
        {
            if ((contentRow.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(contentRow, EntityState.Added);
            }
            else
            {
                this.ObjectContext.ContentRows.AddObject(contentRow);
            }
        }

        public void UpdateContentRow(ContentRow currentContentRow)
        {
            this.ObjectContext.ContentRows.AttachAsModified(currentContentRow, this.ChangeSet.GetOriginal(currentContentRow));
        }

        public void DeleteContentRow(ContentRow contentRow)
        {
            if ((contentRow.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(contentRow, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.ContentRows.Attach(contentRow);
                this.ObjectContext.ContentRows.DeleteObject(contentRow);
            }
        }

        [Query(IsDefault = true)]
        public IQueryable<Unit> GetUnits()
        {
            return this.ObjectContext.Units.OrderBy(t => t.UnitID);
        }

        public void InsertUnit(Unit unit)
        {
            if ((unit.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(unit, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Units.AddObject(unit);
            }
        }

        public void UpdateUnit(Unit currentUnit)
        {
            this.ObjectContext.Units.AttachAsModified(currentUnit, this.ChangeSet.GetOriginal(currentUnit));
        }

        public void DeleteUnit(Unit unit)
        {
            if ((unit.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(unit, EntityState.Deleted);
            }
            else
            {
                this.ObjectContext.Units.Attach(unit);
                this.ObjectContext.Units.DeleteObject(unit);
            }
        }
    }
}


